package com.sec.android.app.myfiles.module.search;

import android.app.LoaderManager;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.recent.RecentAdapterImp;
import com.sec.android.app.myfiles.module.local.recent.RecentDbTableInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;

public class RecentSearchAdapter extends SearchAdapter {
    public RecentSearchAdapter(Context context, NavigationInfo navigationInfo, LoaderManager loaderManager) {
        super(context, navigationInfo, loaderManager);
        mSearchDbTableInfo = RecentDbTableInfo.getInstance();
    }

    @Override
    protected Cursor createGroupCursor() {
        MatrixCursor groupCursor = new MatrixCursor(GROUP_PROJECTION);
        groupCursor.addRow(new String[]{
                String.valueOf(SEARCH_STORAGE_ID.RECENT), mContext.getString(R.string.subtitle_recent)});

        return groupCursor;
    }

    @Override
    protected Uri getUri(int id) {
        return Uri.parse(mSearchDbTableInfo.getUri());
    }

    @Override
    protected String getSelection(int id) {
        StringBuilder selection = new StringBuilder();
        addSelectionQuery(selection);

        if (!mShowHidden) {
            selection.append("AND (").append(mSearchDbTableInfo.getColumnName(COLUMN_ID.NAME))
                    .append(" NOT LIKE '.%')");
        }

        return selection.toString();
    }

    @Override
    protected String getOrderBy(int id) {
        return mSearchDbTableInfo.getColumnName(COLUMN_ID.RECENT_DATE) + " DESC";
    }

    @Override
    public FileRecord getLocalFileRecord(Cursor cursor) {
        return RecentAdapterImp.getRecentFileRecord(cursor, mSearchDbTableInfo);
    }
}
